package com.hqyj.javaSpringBoot.modules.test.dao;

import com.hqyj.javaSpringBoot.modules.test.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface StudentRepository extends JpaRepository<Student, Integer>, JpaSpecificationExecutor<Student> {
    // find + Top || First(option) + By + 属性 + 关键字 + 属性(option)
    //      如果想查前两条数据可用Top; 如果想查第一条数据就用First;也可以不用
    Student findFirstByNameAndEmail(String name, String email);

    /**
     *
     * @param id 导spring包的 @param
     *   HQL
     * @return
     */
    @Query(value = "from Student where id = :id")
    Student getStudentById(@Param("id") int id);

    /**
     * SQL写法，加 nativeQuery = true
     * @param id
     * @return
     */
    @Query(nativeQuery = true, value = "select * from test_student where id = :id")
    Student getStudentByIdForSql(@Param("id") int id);

    /**
     * 增删改写法
     * @Modifying 写了可以辨别增删改，就不会认为是查询
     */
    @Query(nativeQuery = true, value = "insert into test_student (name, email, age)" +
            "values(:#{#student.name}, :#{#student.email}, :#{#student.age})")
    @Modifying
    void insertStudent(@Param("student") Student student);
}
